package com.party.web.web.controller.wechat;

import com.party.common.paging.Page;
import com.party.common.utils.StringUtils;
import com.party.core.model.member.MemberGroup;
import com.party.core.model.wechat.WechatMessage;
import com.party.core.service.wechat.IWechatMessageService;
import com.party.core.service.wechatPlatform.IWechatOpenMemberService;
import com.party.web.utils.RealmUtils;
import com.party.web.web.dto.AjaxResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
@RequestMapping("/wechat/keywordresponse")
public class KeywordResponseController {

	private static Logger log = LoggerFactory.getLogger(KeywordResponseController.class);

	@Autowired
	private IWechatMessageService iWechatMessageService;

	@Autowired
	private IWechatOpenMemberService wechatOpenMemberService;
	
//	关键词回复-设置关键词
	@RequestMapping("view")
	public ModelAndView keywordView(Page page,WechatMessage wechatMessage) {
		//授权判定
		MemberGroup member = RealmUtils.getCurrentUser();
		boolean isAuthorize = wechatOpenMemberService.isAuthorize(member.getId());
		if(!isAuthorize){
			//没有授权
			return new ModelAndView("wechat/boundTips");
		}

		ModelAndView mv = new ModelAndView("wechat/keywordResponse");
        mv.addObject("key",wechatMessage.getKey());
		//页面设置
		page.setLimit(10);
		mv.addObject("page",page);

		//微信回复类型设置 3-关键词回复
		wechatMessage.setType(3);

		//设置当前用户id
		wechatMessage.setMemberId(member.getId());

		List<WechatMessage> list = iWechatMessageService.listPage(wechatMessage,page);
		mv.addObject("responses",list);

		return mv;
	}

	/**
	 * 编辑/新建 关键词回复
	 * @param id 关键词回复的id
	 * @return 页面数据
	 */
	@RequestMapping("keywordform")
	public ModelAndView keywordForm(String id){
		//授权判定
		MemberGroup member = RealmUtils.getCurrentUser();
		boolean isAuthorize = wechatOpenMemberService.isAuthorize(member.getId());
		if(!isAuthorize){
			//没有授权
			return new ModelAndView("wechat/boundTips");
		}
		ModelAndView mv = new ModelAndView("wechat/keywordForm");
		if(StringUtils.isNotEmpty(id)){
			//编辑
			WechatMessage wechatMessage = iWechatMessageService.get(id);
			mv.addObject("response",wechatMessage);
		}
		return mv;
	}
	
//	关键词回复-触发关键词数据
//	@RequestMapping("data")
//	public ModelAndView keywordData() {
//		ModelAndView mv = new ModelAndView("wechat/keywordResponseData");
//		return mv;
//	}

	/**
	 * 删除关键词回复
	 * @param id 关键词回复的id
	 * @return 操作结果
	 */
	@ResponseBody
	@RequestMapping("delete")
	public AjaxResult deleteKeyValue(String id){
		//授权判定
		MemberGroup member = RealmUtils.getCurrentUser();
		boolean isAuthorize = wechatOpenMemberService.isAuthorize(member.getId());
		if(!isAuthorize){
			//没有授权
			return new AjaxResult(false);
		}
		if(StringUtils.isEmpty(id)){
			return new AjaxResult(false);
		}else{
			iWechatMessageService.delete(id);
			return new AjaxResult(true);
		}
	}

	/**
	 * 保存关键词回复信息
	 * @param
	 * @return 操作结果
	 */
	@ResponseBody
	@RequestMapping("save")
	public AjaxResult saveKeyValue(WechatMessage wechatMessage){
		//授权判定
		MemberGroup member = RealmUtils.getCurrentUser();
		boolean isAuthorize = wechatOpenMemberService.isAuthorize(member.getId());
		if(!isAuthorize){
			//没有授权
			return new AjaxResult(false);
		}

		AjaxResult ajaxResult = new AjaxResult();
		if(StringUtils.isEmpty(wechatMessage.getId())){
			//新建关键词回复
			wechatMessage.setMemberId(member.getId());
			iWechatMessageService.insert(wechatMessage);
			ajaxResult.setSuccess(true);
		}else{
			//修改关键词回复
			iWechatMessageService.update(wechatMessage);
			ajaxResult.setSuccess(true);
		}
		return ajaxResult;
	}
}
